package com.yh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.javabean.Permission;
import com.yh.javabean.Product;
import com.yh.javabean.Role;
import com.yh.javabean.Role_Pid;
import com.yh.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: als
 * @Date: 2024/5/27 - 05 - 27 - 16:16
 * @Description: com.yh.controller
 * @version: 1.0
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                          @RequestParam(value = "limit", required = false, defaultValue = "5") int limit,
                          HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        List<Role> all = roleService.findAll();
        PageInfo<Role> pageInfo = new PageInfo<>(all);
        request.setAttribute("pageInfo", pageInfo);
        return "/role/role-list";
    }


    @RequestMapping("/show")
    public String showPage(int id, HttpServletRequest request) {
        Role role = roleService.findById(id);
        List<Permission> list = roleService.permissionMy(id);
       role.setPermissions(list);
        request.setAttribute("role", role);
        return "/role/role-show";
    }

    @GetMapping("/add")
    public String addpage() {
        return "/role/role-add";
    }

    @PostMapping("/add")
    public String add(Role role) {
        roleService.addRole(role);
        return "redirect:findAll";
    }

    @GetMapping("/edit")
    public String editpage(int id,HttpServletRequest request){
        Role role = roleService.findById(id);
        request.setAttribute("role",role);
        return "/role/role-edit";
    }

    @PostMapping("/edit")
    public String editpage(Role role) {
        roleService.editRole(role);
        return "redirect:findAll";
    }


    @GetMapping("/permissionAdd")
    public String permissionAddPage(int id,HttpServletRequest request){
        List<Permission> myPermission = roleService.permissionMy(id);
        List<Permission> list = roleService.PermissionAdd();
        request.setAttribute("permissionList",list);
        request.setAttribute("permissionIdList",myPermission);
        request.setAttribute("roleId",id);
        return "/role/role-edit-permission";
    }


    @PostMapping("/permissionAdd")
    @ResponseBody
    public String permissionAddPage(@RequestBody Role_Pid rp){
      roleService.editPermission(rp);
        return "/role/findAll";
    }

   //删除角色
    @GetMapping("/delete")
    public String delete(int id) {
        roleService.deleteRole(id);
        return "redirect:findAll";
    }

    //角色批量删除
    @PostMapping("/deleteIds")
    @ResponseBody
    public String deleteIds(@RequestBody List<Integer> ids){
        roleService.deleteIds(ids);
        return "/role/findAll";
    }

}
